package mamikeji.liaowa.ui.activity.article;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AbsListView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import mamikeji.liaowa.R;
import mamikeji.liaowa.application.MyApplication;
import mamikeji.liaowa.bean.ArticleDetailsBean;
import mamikeji.liaowa.bean.ArticleDetailsCommentBean;
import mamikeji.liaowa.http_api.ForHttp;
import mamikeji.liaowa.http_api.HttpApi;
import mamikeji.liaowa.http_api.HttpKey;
import mamikeji.liaowa.ui.activity.BaseActivity;
import mamikeji.liaowa.ui.adapter.Article_DetailsAdapter;
import mamikeji.liaowa.ui.adapter.ListViewEmptyAdapter;
import mamikeji.liaowa.utils.ForContents;
import mamikeji.liaowa.utils.ForLog;
import mamikeji.liaowa.utils.ForPreference;
import mamikeji.liaowa.utils.ForToast;
import mamikeji.liaowa.utils.MaxTextLength;
import mamikeji.liaowa.utils.MultiStateView;
import mamikeji.liaowa.utils.SystemUtils;
import mamikeji.liaowa.widget.MyWebView;
import mamikeji.liaowa.widget.ObservableListView;
import mamikeji.liaowa.widget.pulltorefresh.PullToRefreshBase;
import mamikeji.liaowa.widget.pulltorefresh.PullToRefreshListView;
import mamikeji.liaowa.widget.refreshView.PtrlDownRefreshFrameLayout;

public class Article_DetailsActivity extends BaseActivity implements View.OnClickListener, ObservableListView.ScrollViewListener {
    private static final String TAG = "Article_DetailsActivity";
    @BindView(R.id.linearLayout_article_header_bg)
    LinearLayout linearLayoutArticleHeaderBg;
    @BindView(R.id.iv_article_header_left)
    ImageView ivArticleHeaderLeft;
    @BindView(R.id.iv_article_header_right_shade)
    ImageView ivArticleHeaderRightShade;
    @BindView(R.id.tv_article_header_title)
    TextView tvArticleHeaderTitle;
    @BindView(R.id.iv_article_header_right_collection)
    ImageView ivArticleHeaderRightCollection;
    @BindView(R.id.listView_article_date)
    PullToRefreshListView listViewArticleDate;
    @BindView(R.id.ptrlDownRefreshFrameLayout_article_date)
    PtrlDownRefreshFrameLayout ptrlDownRefreshFrameLayoutArticleDate;
    @BindView(R.id.editText_article_recomment)
    EditText editTextArticleRecomment;
    @BindView(R.id.textView_article_recomment_release)
    TextView textViewArticleRecommentRelease;
    @BindView(R.id.multiStateView_article_details)
    MultiStateView multiStateViewArticleDetails;

    private View header_view, header_view_2;
    private Article_DetailsAdapter article_detailsAdapter;

    private SimpleDraweeView imageView_article_details_header;
    private TextView tv_article_details_title;
    private RelativeLayout relativeLayout_article_details_zhuanlan;

    private MyWebView webView_article_details;
    private ProgressBar progressBar_article_details;

    private String articleId;

    private List<ArticleDetailsCommentBean.DataEntity> dataEntityList = new ArrayList<>();
    private List<ArticleDetailsCommentBean.DataEntity> mList;
    private EditText editText_article_recomment;
    private TextView textView_article_recomment_release;
    private boolean isLoaded = true;
    /**
     * 当前页数
     */
    private int page = 1;
    /**
     * 总页数
     */
    private int maxPage = 0;

    private ArticleDetailsCommentBean articleDetailsCommentBean;

    private Dialog dialog_share;

    private ArticleDetailsBean articleDetailsBean;

    private boolean bool_progress = false;

    private int scrolled;//记录当前滚动位置

    private boolean bool_article = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_article_details);
        ButterKnife.bind(this);
        getBase();
        tileBar(this, true);
        Intent intent = getIntent();
        articleId = intent.getStringExtra("articleId");

        init();
        initDate();
        getDialogShare();
    }

    private void init() {
        editText_article_recomment = (EditText) findViewById(R.id.editText_article_recomment);
        InputFilter[] filters = new InputFilter[]{new MaxTextLength(400)};
        editText_article_recomment.setFilters(filters);

        textView_article_recomment_release = (TextView) findViewById(R.id.textView_article_recomment_release);

        header_view = LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.item_article_details_header, null);
        listViewArticleDate.getRefreshableView().addHeaderView(header_view);

        imageView_article_details_header = (SimpleDraweeView) header_view.findViewById(R.id.imageView_article_details_header);
        tv_article_details_title = (TextView) header_view.findViewById(R.id.tv_article_details_title);
        relativeLayout_article_details_zhuanlan = (RelativeLayout) header_view.findViewById(R.id.relativeLayout_article_details_zhuanlan);

        header_view_2 = LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.item_article_details_header_2, null);
        listViewArticleDate.getRefreshableView().addHeaderView(header_view_2);
        webView_article_details = (MyWebView) header_view_2.findViewById(R.id.webView_article_details);
        progressBar_article_details = (ProgressBar) header_view_2.findViewById(R.id.progressBar_article_details);

        article_detailsAdapter = new Article_DetailsAdapter(dataEntityList, this);

        relativeLayout_article_details_zhuanlan.setOnClickListener(this);
        ivArticleHeaderLeft.setOnClickListener(this);
        ivArticleHeaderRightShade.setOnClickListener(this);
        tvArticleHeaderTitle.setOnClickListener(this);
        ivArticleHeaderRightCollection.setOnClickListener(this);
        textView_article_recomment_release.setOnClickListener(this);

        multiStateViewArticleDetails.getView(MultiStateView.VIEW_STATE_ERROR).findViewById(R.id.button_error_refish)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        multiStateViewArticleDetails.setViewState(MultiStateView.VIEW_STATE_LOADING);
                        initDate();
                    }
                });

        ptrlDownRefreshFrameLayoutArticleDate.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, listViewArticleDate.getRefreshableView(), header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                if (bool_progress)
                    initDate();
            }
        });
        listViewArticleDate.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        listViewArticleDate.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            //下拉刷新数据
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
            }

            //上拉加载更多
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                if (++page >= maxPage) {
                    new Handler().postDelayed(new Runnable() {
                        public void run() {
                            listViewArticleDate.onRefreshComplete();
                            ForToast.showToast(R.string.error_down_end);
                        }
                    }, 1000);
                } else {
                    initRecommentDate(page, 10);
                }
            }
        });

        article_detailsAdapter.setOnItemClickListener(new Article_DetailsAdapter.OnItemClickListener() {
            @Override
            public void onZanClick(int position, boolean isFabulous, int commentId) {
                if (getJudgeLogin()) {
                    if (isFabulous) {
                        getCanelArticleComment(commentId, position);
                    } else {
                        getZanArticleComment(commentId, position);
                    }
                }
            }
        });

        listViewArticleDate.getRefreshableView().setOnScrollListener(new AbsListView.OnScrollListener() {
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                linearLayoutArticleHeaderBg.setBackgroundResource(R.color.color_app);
                // 判断滚动到顶部
                if (listViewArticleDate.getRefreshableView().getFirstVisiblePosition() == 0) {
                    linearLayoutArticleHeaderBg.setBackgroundResource(R.color.color_00000000);
                }

                // 不滚动时保存当前滚动到的位置
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    scrolled = listViewArticleDate.getRefreshableView().getFirstVisiblePosition();
                }
            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {

            }
        });
    }

    private void initDate() {
        //参数集合
        ForHttp.getMsg(HttpApi.ROOT_PATH + HttpApi.ARTICLE_DETAILS
                        + "?articleId=" + articleId
                        + "&usercode=" + ForPreference.getString(ForContents.USER_CODE)
                , new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        ForLog.i(TAG, e.getMessage());
                        ForToast.showToast(R.string.intent_error);
                        multiStateViewArticleDetails.setViewState(MultiStateView.VIEW_STATE_NOINTERNET);
                        ptrlDownRefreshFrameLayoutArticleDate.refreshComplete();
                    }

                    @Override
                    public void onResponse(String response) {
                        ForLog.e(TAG, response);
                        ptrlDownRefreshFrameLayoutArticleDate.refreshComplete();
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            String status = jsonObject.getString(HttpKey.STATUS);
                            if ("200".equals(status)) {
                                articleDetailsBean = new Gson().fromJson(response, ArticleDetailsBean.class);
                                if (articleDetailsBean.getData() != null) {
                                    if (articleDetailsBean.getData().getImgurl() != null || articleDetailsBean.getData().getImgurl().equals("")) {
                                        imageView_article_details_header.setImageURI(Uri.parse(articleDetailsBean.getData().getImgurl()));
                                        tv_article_details_title.setText(articleDetailsBean.getData().getTitle());

                                        getWebView(HttpApi.ARTICLE_H5_URL + articleDetailsBean.getData().getArticleId() + HttpApi.ARTICLE_H5_KEY,
                                                articleDetailsBean.getData().getContent());

                                        ivArticleHeaderRightCollection
                                                .setImageResource(articleDetailsBean.getData().getIsCollection() == 1
                                                        ? R.drawable.icon_shoucang_h : R.drawable.icon_shoucang_n);

                                        initRecommentDate(1, 5);
                                        multiStateViewArticleDetails.setViewState(MultiStateView.VIEW_STATE_CONTENT);
                                    }
                                } else {
                                    multiStateViewArticleDetails.setViewState(MultiStateView.VIEW_STATE_EMPTY);
                                }
                            } else {
                                multiStateViewArticleDetails.setViewState(MultiStateView.VIEW_STATE_ERROR);
                                ForToast.showToast(jsonObject.getString(HttpKey.MESSAGE));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void initRecommentDate(final int page, int count) {
        ForHttp.getMsg(HttpApi.ROOT_PATH + HttpApi.ARTICLE_DETAILS_RECOMMENT
                        + "?articleId=" + articleId
                        + "&usercode=" + ForPreference.getString(ForContents.USER_CODE)
                        + "&page=" + page + "&pageSize=" + count
                , new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        listViewArticleDate.onRefreshComplete();
                        ForLog.i(TAG, e.getMessage());
                        ForToast.showToast(R.string.intent_error);
                    }

                    @Override
                    public void onResponse(String response) {
                        listViewArticleDate.onRefreshComplete();
                        ForLog.e(TAG + "recomment", response);
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            String status = jsonObject.getString(HttpKey.STATUS);
                            if ("200".equals(status)) {
                                articleDetailsCommentBean = new Gson().fromJson(response, ArticleDetailsCommentBean.class);
                                mList = articleDetailsCommentBean.getData();

                                if (mList != null && mList.size() > 0) {
                                    if (page == 1) {
                                        dataEntityList.clear();
                                        dataEntityList.addAll(mList);
                                        article_detailsAdapter.setList(dataEntityList);
                                        listViewArticleDate.setAdapter(article_detailsAdapter);
                                    } else {
                                        dataEntityList.addAll(mList);
                                        article_detailsAdapter.notifyDataSetChanged();
                                    }
                                } else {
                                    if (page == 1) {
                                        listViewArticleDate.setAdapter(new ListViewEmptyAdapter(Article_DetailsActivity.this, "暂无评论~~~"));
                                    }
                                }

                            } else {
                                ForToast.showToast(jsonObject.getString(HttpKey.MESSAGE));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void getWebView(final String url2, final String string) {
        WebSettings webSettings = webView_article_details.getSettings();
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        // 设置WebView属性，能够执行JavaScript脚本
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        // 设置可以支持缩放
        webSettings.setSupportZoom(true);
        // 设置出现缩放工具
        //自适应屏幕
        //webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        //webSettings.setLoadWithOverviewMode(true);

        webView_article_details.setHorizontalScrollBarEnabled(false);//禁止水平滚动
        webView_article_details.setVerticalScrollBarEnabled(true);//允许垂直滚动

        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webView_article_details.setVerticalScrollBarEnabled(false);
        webView_article_details.setVerticalScrollbarOverlay(false);
        webView_article_details.setHorizontalScrollBarEnabled(false);
        webView_article_details.setHorizontalScrollbarOverlay(false);


        webView_article_details.loadUrl(url2);

        webView_article_details.setOnSizeChanged(new MyWebView.OnSizeChangedListener() {
            @Override
            public void sizeChanged(int h) {
                webView_article_details.measure(1080, h);
            }
        });

        webView_article_details.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (isLoaded) {
                    isLoaded = false;
                    getWebView(url2, string);
                }

            }
        });

        webView_article_details.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {
                    progressBar_article_details.setVisibility(View.GONE);//加载完网页进度条消失
                    bool_progress = true;
                } else {
                    progressBar_article_details.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                    progressBar_article_details.setProgress(newProgress);//设置进度值
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.relativeLayout_article_details_zhuanlan:
                MobclickAgent.onEvent(this,"FeatareArticlesColumnButton");//专题文章-专栏按钮
                startActivity(new Intent(Article_DetailsActivity.this, ArticleTypeActivity.class));
                break;
            case R.id.iv_article_header_left:
                if (bool_article){
                    setResult(RESULT_OK, new Intent());
                }
                finish();
                break;
            case R.id.iv_article_header_right_shade:
                MobclickAgent.onEvent(this,"FeatareArticlesShore");//专题文章-分享
                dialog_share.show();
                break;
            case R.id.iv_article_header_right_collection:
                MobclickAgent.onEvent(this,"FeatareArticlesCollection");//专题文章-收藏
                if (getJudgeLogin()) {
                    if (articleDetailsBean.getData().getIsCollection() == 1) {
                        deleteCollectionDate();//取消收藏
                    } else {
                        initCollectionDate();//收藏文章
                    }
                }
                break;
            case R.id.textView_article_recomment_release:
                if (getJudgeLogin()) {
                    if (TextUtils.isEmpty(editText_article_recomment.getText().toString())) {
                        ForToast.showToast(R.string.aiticle_details_recomment_error);
                        return;
                    }
                    getRelease(editText_article_recomment.getText().toString());
                }
                break;
            case R.id.linearLayout_share_wechatFriend:
                dialog_share.hide();
                ShareWeb(SHARE_MEDIA.WEIXIN);
                break;
            case R.id.linearLayout_share_wechatCircle:
                dialog_share.hide();
                ShareWeb(SHARE_MEDIA.WEIXIN_CIRCLE);
                break;
            case R.id.linearLayout_share_tencentCircle:
                dialog_share.hide();
                ShareWeb(SHARE_MEDIA.QZONE);
                break;
        }
    }

    /**
     * 收藏文章
     */
    private void initCollectionDate() {
        //参数集合
        Map<String, String> formMap = new HashMap<String, String>();
        formMap.put(HttpKey.COLLECTION_ARTICLE_USERCODE, ForPreference.getString(ForContents.USER_CODE));
        formMap.put(HttpKey.COLLECTION_ARTICLE_ARTICLEID, articleId);
        ForHttp.setMsg(HttpApi.ROOT_PATH + HttpApi.COLLECTION_ARTICLE, formMap, new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                ForLog.i(TAG, e.getMessage());
                ForToast.showToast(R.string.intent_error);
            }

            @Override
            public void onResponse(String response) {
                ForLog.e(TAG, response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String status = jsonObject.getString(HttpKey.STATUS);
                    if ("200".equals(status)) {
                        bool_article = true;
                        int num = ForPreference.getInt(ForContents.USER_COLLECTION);
                        //记录收藏数量
                        ForPreference.putInt(ForContents.USER_COLLECTION, ++num);
                        ForToast.showToast(R.string.collection_success);
                        ivArticleHeaderRightCollection.setImageResource(R.drawable.icon_shoucang_h);
                    } else {
                        ForToast.showToast(jsonObject.getString(HttpKey.MESSAGE));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 取消文章收藏
     */
    private void deleteCollectionDate() {
        //参数集合
        Map<String, String> formMap = new HashMap<String, String>();
        formMap.put(HttpKey.COLLECTION_ARTICLE_USERCODE, ForPreference.getString(ForContents.USER_CODE));
        formMap.put(HttpKey.COLLECTION_ARTICLE_ARTICLEID, articleId);
        ForHttp.setMsg(HttpApi.ROOT_PATH + HttpApi.COLLECTION_DELETE, formMap, new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                ForLog.i(TAG, e.getMessage());
                ForToast.showToast(R.string.intent_error);
            }

            @Override
            public void onResponse(String response) {
                ForLog.e(TAG, response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String status = jsonObject.getString(HttpKey.STATUS);
                    if ("200".equals(status)) {
                        bool_article = true;
                        int num = ForPreference.getInt(ForContents.USER_COLLECTION);
                        //记录收藏数量
                        ForPreference.putInt(ForContents.USER_COLLECTION, --num);
                        ForToast.showToast(R.string.collection_delete);
                        ivArticleHeaderRightCollection.setImageResource(R.drawable.icon_shoucang_n);
                    } else {
                        ForToast.showToast(jsonObject.getString(HttpKey.MESSAGE));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 评论文章
     */
    private void getRelease(String content) {
        showLoading();
        //参数集合
        Map<String, String> formMap = new HashMap<String, String>();
        formMap.put(HttpKey.RECOMMENT_ARTICLE_USERCODE, ForPreference.getString(ForContents.USER_CODE));
        formMap.put(HttpKey.RECOMMENT_ARTICLE_ARTICLEID, articleId);
        formMap.put(HttpKey.RECOMMENT_ARTICLE_CONTENT, content);
        ForHttp.setMsg(HttpApi.ROOT_PATH + HttpApi.RECOMMENT_ARTICLE, formMap, new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                ForLog.i(TAG, e.getMessage());
                ForToast.showToast(R.string.intent_error);
                hideLoading();
            }

            @Override
            public void onResponse(String response) {
                ForLog.e(TAG, response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String status = jsonObject.getString(HttpKey.STATUS);
                    if ("200".equals(status)) {
                        ForToast.showToast(R.string.aiticle_details_recomment_success);
                        editText_article_recomment.setText("");
                        isKeyboardShownToHideKeyboard();
                        initRecommentDate(1, 5);
                        listViewArticleDate.getRefreshableView().setSelection(scrolled);
                        ForLog.i("scrolledscrolled：","yes");
                    } else {
                        ForToast.showToast(jsonObject.getString(HttpKey.MESSAGE));
                    }
                    hideLoading();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void getZanArticleComment(int commentId, final int position) {
        //参数集合
        Map<String, String> formMap = new HashMap<String, String>();
        formMap.put(HttpKey.FABULOUS_ARTICLE_USERCODE, ForPreference.getString(ForContents.USER_CODE));
        formMap.put(HttpKey.FABULOUS_ARTICLE_COMMENTID, commentId + "");
        formMap.put(HttpKey.FABULOUS_ARTICLE_ARTICLEID, articleId);
        ForHttp.setMsg(HttpApi.ROOT_PATH + HttpApi.FABULOUS_ARTICLE_DETAILS, formMap, new StringCallback() {

            @Override
            public void onError(Request request, Exception e) {
                ForLog.i(TAG, e.getMessage());
                ForToast.showToast(R.string.intent_error);
            }

            @Override
            public void onResponse(String response) {
                ForLog.e(TAG, response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String status = jsonObject.getString(HttpKey.STATUS);
                    if ("200".equals(status)) {
                        ArticleDetailsCommentBean.DataEntity dataEntity = article_detailsAdapter.getList().get(position);
                        dataEntity.setFabulousCount(dataEntity.getFabulousCount() + 1);
                        dataEntity.setIsFabulous(true);
                        article_detailsAdapter.notifyDataSetChanged();
                    } else {
                        ForToast.showToast(jsonObject.getString(HttpKey.MESSAGE));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void getCanelArticleComment(int commentId, final int position) {
        //参数集合
        Map<String, String> formMap = new HashMap<String, String>();
        formMap.put(HttpKey.FABULOUS_ARTICLE_USERCODE, ForPreference.getString(ForContents.USER_CODE));
        formMap.put(HttpKey.FABULOUS_ARTICLE_COMMENTID, commentId + "");
        formMap.put(HttpKey.FABULOUS_ARTICLE_ARTICLEID, articleId);
        ForHttp.setMsg(HttpApi.ROOT_PATH + HttpApi.FABULOUS_ARTICLE_DETAILS_canel, formMap, new StringCallback() {

            @Override
            public void onError(Request request, Exception e) {
                ForLog.i(TAG, e.getMessage());
                ForToast.showToast(R.string.intent_error);
            }

            @Override
            public void onResponse(String response) {
                ForLog.e(TAG, response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String status = jsonObject.getString(HttpKey.STATUS);
                    if ("200".equals(status)) {
                        ArticleDetailsCommentBean.DataEntity dataEntity = article_detailsAdapter.getList().get(position);
                        dataEntity.setFabulousCount(dataEntity.getFabulousCount() - 1);
                        dataEntity.setIsFabulous(false);
                        article_detailsAdapter.notifyDataSetChanged();
                    } else {
                        ForToast.showToast(jsonObject.getString(HttpKey.MESSAGE));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 分享弹框
     */
    public void getDialogShare() {
        dialog_share = new Dialog(this, R.style.ActionSheetDialogStyle);
        View view = LayoutInflater.from(this).inflate(R.layout.diglog_set_shape, null);
        dialog_share.setContentView(view);
        Window dialogWindow = dialog_share.getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.y = 0;
        lp.width = (int) getResources().getDisplayMetrics().widthPixels; // 宽度
        dialogWindow.setAttributes(lp);

        LinearLayout linearLayout_share_wechatFriend = (LinearLayout) view.findViewById(R.id.linearLayout_share_wechatFriend);
        LinearLayout linearLayout_share_wechatCircle = (LinearLayout) view.findViewById(R.id.linearLayout_share_wechatCircle);
        LinearLayout linearLayout_share_tencentCircle = (LinearLayout) view.findViewById(R.id.linearLayout_share_tencentCircle);

        linearLayout_share_wechatFriend.setOnClickListener(Article_DetailsActivity.this);
        linearLayout_share_wechatCircle.setOnClickListener(Article_DetailsActivity.this);
        linearLayout_share_tencentCircle.setOnClickListener(Article_DetailsActivity.this);
    }

    /**
     * 分享回调
     *
     * @param anEnum
     */
    private void ShareWeb(Enum anEnum) {
        UMImage thumb = new UMImage(this, articleDetailsBean.getData().getImgurl());
        UMWeb web = new UMWeb(HttpApi.ROOT_PATH_ + articleDetailsBean.getData().getContent());
        web.setThumb(thumb);
        web.setDescription(articleDetailsBean.getData().getTitle());
        web.setTitle(articleDetailsBean.getData().getCategoryName());
        new ShareAction(this).withMedia(web).setPlatform((SHARE_MEDIA) anEnum).setCallback(new UMShareListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {
                ForToast.showToast("后台正在玩儿命加载~~~");
            }

            @Override
            public void onResult(SHARE_MEDIA share_media) {
                ForToast.showToast("分享成功！");
            }

            @Override
            public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                ForLog.i("throwable", "" + throwable.toString());
                ForToast.showToast("分享失败！");
            }

            @Override
            public void onCancel(SHARE_MEDIA share_media) {
                ForToast.showToast("取消分享！");
            }
        }).share();
    }

    /**
     * 判断软键盘是否弹起如弹起则隐藏
     */
    private void isKeyboardShownToHideKeyboard() {
        if (SystemUtils.isKeyboardShown(editText_article_recomment.getRootView())) {
            SystemUtils.hideKeyboard(this, editText_article_recomment.getApplicationWindowToken());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        //清空所有Cookie
        CookieSyncManager.createInstance(this);  //Create a singleton CookieSyncManager within a context
        CookieManager cookieManager = CookieManager.getInstance(); // the singleton CookieManager instance
        cookieManager.removeAllCookie();// Removes all cookies.
        CookieSyncManager.getInstance().sync(); // forces sync manager to sync now

        webView_article_details.setWebChromeClient(null);
        webView_article_details.setWebViewClient(null);
        webView_article_details.getSettings().setJavaScriptEnabled(false);
        webView_article_details.clearCache(true);
        webView_article_details.destroy();
    }

    @Override
    public void onScrollChanged(ObservableListView scrollView, int x, int y, int oldx, int oldy) {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (bool_article){
                setResult(RESULT_OK, new Intent());
            }
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("专题文章");
        MobclickAgent.onResume(this);       //开始统计时长
    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("专题文章");
        MobclickAgent.onPause(this);       //结束统计时长
    }
}
